package com.prs.crm.dao.oa.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.AfficheDao;
import com.prs.crm.domain.oa.Affiche;

public class AfficheDaoImpl extends BaseDaoImpl<Affiche> implements AfficheDao{

	@Override
	public Collection<Affiche> getAllAffiche() {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Affiche.class);
		criteria.addOrder(Order.desc("recordTime"));
		return criteria.list();
	}

	@Override
	public Collection<Affiche> getAfficheByIds(Collection<Integer> ids) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Affiche.class);
		criteria.add(Restrictions.in("id", ids));
		criteria.addOrder(Order.desc("recordTime"));
		return criteria.list();
	}

	@Override
	public Collection<Affiche> getAfficheByUserCount(int pageCount) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Affiche.class);
		criteria.addOrder(Order.desc("recordTime"));
		return criteria.setMaxResults(pageCount).list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Affiche> getAffichesByType(final String type) {
		// TODO Auto-generated method stub
		return (Collection<Affiche>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Affiche.class);
				criteria.add(Restrictions.eq("type", type));
				criteria.addOrder(Order.desc("recordTime"));
				return criteria.list();
			}
			
		});
	}
}
